package com.authService.service.impl;

import com.authService.convert.RoleConvert;
import com.authService.entity.SysRoleEntity;
import com.authService.enums.OperationLogTypeEnum;
import com.authService.mapper.SysRoleMapper;
import com.authService.pojo.req.RolePageReqDTO;
import com.authService.pojo.req.RoleSaveReqDTO;
import com.authService.pojo.res.RoleDetialResDTO;
import com.authService.pojo.res.RolePageResDTO;
import com.authService.service.ISysRoleService;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.operationLog.pojo.bo.OperationLogBO;
import com.common.operationLog.service.IOperationLogService;
import com.xiaopeng.common.page.PageUtil;
import com.xiaopeng.common.page.PageVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author xiaopeng
 * @since 2024-10-21
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements ISysRoleService {

    @Resource
    private IOperationLogService operationLogService;

    @Override
    public PageVO<RolePageResDTO> selectPage(RolePageReqDTO reqDTO) {
        return PageUtil.convertPages(reqDTO, baseMapper::selectRoleList);
    }

    @Override
    public RoleDetialResDTO detail(Long roleId) {
        SysRoleEntity roleEntity = this.getById(roleId);
        return RoleConvert.INSTANCE.detailConvert(roleEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(RoleSaveReqDTO reqDTO) {
        SysRoleEntity roleEntity = RoleConvert.INSTANCE.saveReqDTOConvert(reqDTO);
        long roleId = IdWorker.getId();
        roleEntity.setId(roleId);
//        roleEntity.setCreateBy(UserHolder.getUserId());
//        roleEntity.setCreateByName(UserHolder.getUserName());
//        roleEntity.setCreateTime(LocalDateTime.now());
//        roleEntity.setUpdateBy(UserHolder.getUserId());
//        roleEntity.setUpdateByName(UserHolder.getUserName());
        roleEntity.setUpdateTime(LocalDateTime.now());
        this.save(roleEntity);
        // 保存操作日志
        RoleDetialResDTO ruleDetail = this.detail(roleId);
        operationLogService.save(OperationLogBO.getInstance()
                .biz(OperationLogTypeEnum.ROLE_SAVE, roleId, null)
                .content(reqDTO, null, ruleDetail));
    }

    @Override
    public void updateRole(RoleSaveReqDTO reqDTO) {
        Long roleId = reqDTO.getRoleId();
        RoleDetialResDTO oldRuleDetail = this.detail(roleId);
        SysRoleEntity updateEntity = RoleConvert.INSTANCE.saveReqDTOConvert(reqDTO);
        updateEntity.setId(reqDTO.getRoleId());
//        updateEntity.setUpdateBy(UserHolder.getUserId());
//        updateEntity.setUpdateByName(UserHolder.getUserName());
        updateEntity.setUpdateTime(LocalDateTime.now());
        this.updateById(updateEntity);
        // 保存操作日志
        RoleDetialResDTO newRuleDetail = this.detail(roleId);
        operationLogService.save(OperationLogBO.getInstance()
                .biz(OperationLogTypeEnum.ROLE_UPDATE, roleId, null)
                .content(reqDTO, oldRuleDetail, newRuleDetail));
    }
}
